package com.group.eams.controller.classes;

import com.group.eams.entity.Discipline;
import com.group.eams.service.IDisciplineService;
import com.group.eams.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.portlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by 巧中 on 2016/11/4.
 */
@Controller
@RequestMapping("/classes/discipline")
public class DisciplineController {

    @Autowired
    private IDisciplineService disciplineService;

    @RequestMapping("/list")
    public ModelAndView disciplineList(){
        ModelAndView modelAndView = new ModelAndView();
        List<Discipline> disciplineList = disciplineService.getDisciplineList();
        modelAndView.addObject("disciplineList",disciplineList);
        modelAndView.setViewName("/classes/discipline/disciplineList");
        return modelAndView;
    }
    @RequestMapping("/detail")
    public ModelAndView detail(String name){
        ModelAndView modelAndView = new ModelAndView("/classes/discipline/detail");
        Discipline discipline = disciplineService.findDisciplineByName(name);
        modelAndView.addObject("discipline",discipline);
        return modelAndView;
    }
//    @RequestMapping("/detail")
//    private ModelAndView detail(int id){
//        ModelAndView modelAndView = new ModelAndView("/classes/discipline/detail");
//        Discipline discipline = disciplineService.findDisciplineById(id);
//        modelAndView.addObject("discipline",discipline);
//        return modelAndView;
//    }
    @RequestMapping("/toEdit")
    public ModelAndView toEditDiscipline(Integer id){
        ModelAndView modelAndView = new ModelAndView();
        if (id == null){
            modelAndView.setViewName("classes/discipline/addDiscipline");
        }else {
            Discipline discipline = disciplineService.findDisciplineById(id);
            modelAndView.setViewName("classes/discipline/editDiscipline");
            modelAndView.addObject("discipline",discipline);
        }
        return modelAndView;
    }
    @RequestMapping("/save")
    public org.springframework.web.servlet.ModelAndView save(@ModelAttribute Discipline discipline, HttpServletRequest request) {
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        org.springframework.web.servlet.ModelAndView modelAndView = new org.springframework.web.servlet.ModelAndView();
        Result result = new Result();
        if (discipline.getId() == null) {
            System.out.println("执行添加");
            System.out.println(discipline.getName());
            discipline.setIsuesd(1);
            result = disciplineService.addDiscipline(discipline);
        } else {
            System.out.println("执行修改");
            result = disciplineService.updateDiscipline(discipline);
        }
        List<Discipline> disciplineList = disciplineService.getDisciplineList();
        modelAndView.addObject("disciplineList", disciplineList);
        modelAndView.addObject("result", result);
        modelAndView.setViewName("/classes/discipline/list");
        return modelAndView;
    }
    @RequestMapping("/delete")
    public Result delete(Integer id){
        Result result = new Result();
        result = disciplineService.deleteDisciplineById(id);
        return result;
    }
}
